package com.netposa.datawarehouse.statistics.utils;

import java.math.BigDecimal;

public class NumberUtils {
   public static double keepPrecision(Double number, int precision) {
      BigDecimal bg = new BigDecimal(number);
      return bg.setScale(precision, 5).doubleValue();
   }

   public static double keepPrecision(Double number) {
      BigDecimal bg = new BigDecimal(number);
      return bg.setScale(2, 5).doubleValue();
   }

   public static double safeDiv(Double a, Double b) {
      return b != null && b != 0.0 ? keepPrecision(a / b) : 0.0;
   }

   public static double safeDiv(Long a, Long b) {
      return b != null && b != 0L ? keepPrecision((double)a / (double)b) : 0.0;
   }

   public static double safeDiv(Long a, Long b, int precision) {
      return b != null && b != 0L ? keepPrecision((double)a / (double)b, precision) : 0.0;
   }
}
